import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //把账号密码存入数据库
        req.setCharacterEncoding("utf8");
        String username=req.getParameter("username");
        String password= req.getParameter("password");
        if(username==null || password==null || username.equals("") || password.equals("")){
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("账号或密码为空");
            resp.setStatus(403);
            return;
        }
        UserDao userDao=new UserDao();
        User user=new User();
        user.setUsername(username);
        user.setPassword(password);
        try {
            userDao.insertUser(user);
            resp.sendRedirect("login.html");
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
